/**
 * 
 */
package etric.mapmaker.output;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import etric.mapmaker.base.OsmMapMaker;

/**
 * creates the .kal calibration file for the Glupos programm.
 * @author etric
 */
public class GlopusKalFile
{
	public void parseMapFile(OsmMapMaker parent, String mapName, String mapPath, double N, double S, double W, double E, int mapWidth, String fileFormat)
	{
		// used files
		//File inputFile  = new File("ozi_stub.map");
		java.io.InputStream inputFile = parent.getClass().getResourceAsStream("/glopus_stub.kal");
		File outputFile = new File(mapPath + "/" + mapName + "_" + fileFormat +".kal");
		
		//create input reader
		BufferedReader br = null;
		InputStreamReader isr = new InputStreamReader(inputFile);
		//br = new BufferedReader(new FileReader(inputFile));
		br = new BufferedReader(isr);
		
		// create output writer
		PrintWriter bw = null;
		try {
			bw = new PrintWriter( new FileWriter(outputFile) );
		} catch (IOException e1) {
			System.err.println("can't create or read output file.");
		}

		// parse template and write new content
		String line = null;
		try
		{
			while((line=br.readLine())!=null)
			{
				line = line.replaceAll("--N--", Double.toString(N));
				line = line.replaceAll("--E--", Double.toString(E));
				line = line.replaceAll("--S--", Double.toString(S));
				line = line.replaceAll("--W--", Double.toString(W));
				line = line.replaceAll("--Filename--", mapName);
				line = line.replaceAll("--Fileformat--", fileFormat);
				line = line.replaceAll("--Width--", Integer.toString(mapWidth));
				bw.println(line);
			}
			
			br.close();
			bw.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
